#!/bin/bash

# DeepGuard ClamAV 连接诊断工具

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查命令是否存在
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# 检测 Docker Compose 命令
detect_compose_cmd() {
    if docker compose version >/dev/null 2>&1; then
        COMPOSE_CMD="docker compose"
        log_info "使用 Docker Compose V2"
    elif command_exists docker-compose; then
        COMPOSE_CMD="docker-compose"
        log_info "使用 Docker Compose V1"
    else
        log_error "Docker Compose 未安装"
        exit 1
    fi
}

echo "🔍 DeepGuard ClamAV 连接诊断工具"
echo "=================================="

# 检测 Docker Compose 命令
detect_compose_cmd

# 进入 Docker 配置目录
cd deep-guard-docker

# 1. 检查容器状态
echo "1. 检查容器状态..."
if docker ps | grep -q deepguard-clamav; then
    log_success "ClamAV容器正在运行"
else
    log_error "ClamAV容器未运行"
fi

echo "📋 容器状态:"
$COMPOSE_CMD ps

# 2. 检查ClamAV容器健康状态
echo ""
echo "2. 检查ClamAV容器健康状态..."
if docker ps --format "table {{.Names}}\t{{.Status}}" | grep deepguard-clamav; then
    health_status=$(docker inspect deepguard-clamav --format='{{.State.Health.Status}}' 2>/dev/null || echo "无健康检查")
    echo "🏥 ClamAV健康状态: $health_status"
else
    log_error "ClamAV容器未找到"
fi

# 3. 检查ClamAV容器日志
echo ""
echo "3. 检查ClamAV容器日志 (最近20行)..."
echo "📝 ClamAV日志:"
$COMPOSE_CMD logs --tail=20 clamav-daemon

# 4. 测试网络连接
echo ""
echo "4. 测试网络连接..."
echo "🌐 从scanner容器测试连接ClamAV:"
if docker exec deepguard-scanner nc -z clamav-daemon 3310 2>/dev/null; then
    log_success "网络连接正常"
else
    log_error "网络连接失败"
fi

# 5. 测试ClamAV端口连接
echo ""
echo "5. 测试ClamAV端口连接..."
if docker exec deepguard-clamav netstat -tuln | grep -q ":3310"; then
    log_success "ClamAV端口3310正在监听"
else
    log_error "ClamAV端口连接失败"
fi

# 6. 检查ClamAV进程
echo ""
echo "6. 检查ClamAV进程..."
echo "🔄 ClamAV容器内进程:"
docker exec deepguard-clamav ps aux | grep -E "(clamd|freshclam)" | grep -v grep

# 7. 检查病毒库状态
echo ""
echo "7. 检查病毒库状态..."
echo "🦠 病毒库文件:"
docker exec deepguard-clamav ls -la /var/lib/clamav/

# 8. 系统资源使用情况
echo ""
echo "8. 系统资源使用情况..."
echo "💾 内存使用:"
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" | grep deepguard-clamav

echo ""
echo "=================================="
echo "🔧 建议的解决步骤:"
echo ""

# 根据容器状态给出建议
if ! docker ps | grep -q deepguard-clamav; then
    log_error "ClamAV容器未运行，建议:"
    echo "   1. 启动服务: $COMPOSE_CMD up -d"
    echo "   2. 检查配置文件是否正确"
    echo "   3. 查看启动日志: $COMPOSE_CMD logs clamav-daemon"
elif docker inspect deepguard-clamav --format='{{.State.Health.Status}}' 2>/dev/null | grep -q "starting"; then
    log_warning "ClamAV容器不健康，建议:"
    echo "   1. 等待更长时间让ClamAV完成初始化"
    echo "   2. 重启ClamAV容器: $COMPOSE_CMD restart clamav-daemon"
    echo "   3. 检查系统内存是否充足 (建议至少2GB可用内存)"
elif ! docker exec deepguard-scanner nc -z clamav-daemon 3310 2>/dev/null; then
    log_warning "网络连接问题，建议:"
    echo "   1. 检查Docker网络配置"
    echo "   2. 重启所有服务: $COMPOSE_CMD down && $COMPOSE_CMD up -d"
    echo "   3. 检查防火墙设置"
else
    log_success "ClamAV运行正常！"
fi

echo ""
echo "🚀 快速修复命令:"
echo "   # 重启ClamAV服务"
echo "   $COMPOSE_CMD restart clamav-daemon"
echo ""
echo "   # 完全重建服务"
echo "   $COMPOSE_CMD down && $COMPOSE_CMD up -d"
echo ""
echo "   # 查看实时日志"
echo "   $COMPOSE_CMD logs -f clamav-daemon"
echo ""
echo "   # 手动更新病毒库"
echo "   docker exec deepguard-clamav freshclam"